#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 18.12.2010

@author: Lukas Deichmann

SLUM - Serious LDAP User Management

Copyright (C) 2010 Lukas Deichmann

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
'''
import ldap, csv, ldap.modlist, string, os, grp, gettext, bz2

printDebug = 1

def Debug(text):
    if printDebug == 1:
        print "DEBUG: " + text

try:
    import appy.pod.renderer
    odtPossible = True
    Debug("The Appy framework is present, .odt files can be exported")
except:
    Debug("Could not find the Appy framework - disabled output of .odt")
    odtPossible = False

try:
   import cPickle as pickle
except:
   import pickle
   
class SLUM:
    
    def __init__(self):
        self.ldapConnection = False
        self.options = {"ldap_ip" : "192.168.140.134", 
                "ldap_user" : "cn=admin", 
                "ldap_password" : "secret", 
                "odt_template" : ""}
        #self.getOptions()
        
    def getOptions(self):
        Debug("Getting Options")
        try:
            f = open(os.path.expanduser("~") + "/.slum", "r")
            self.options = pickle.load(f)
            self.password = bz2.decompress(self.options.ldappw)
            f.close()
        except:
            self.options = {"ldap_ip" : "12.13.14.15", 
                            "ldap_user" : "cn=admin", 
                            "ldap_password" : "", 
                            "odt_template" : ""}
            print self.options["ldap_ip"]
            Debug("No configuration file found, using defaults")
            
    def saveOptions(self):
        print "Speichere die Optionen"

        try:
 #           os.remove(os.path.expanduser("~") + "/.hgg_tools.config")
            f = file(os.path.expanduser("~") + "/.hgg_tools.config", 'w')
            pickle.dump(self.options, f)
            f.close()
        except:
            Debug("nawt wurkin")

#    def addEntry(self, entry):

        
    def _openLDAP(self):
        if self.ldapConnection == False:
            try:
                self.l = ldap.open(self.options["ldapip"])
                self.l.simple_bind_s(self.options["ldapusr"], self.options["ldappw"])
                print "Erfolgreich verbunden.\n"
                self.ldapConnection = True
            except ldap.LDAPError, error_message:
                print error_message
        else:
            Debug("LDAP Verbindung besteht bereits")        
    
    def _closeLDAP(self):
        if self.ldapConnection == True:        
            print "Schließe LDAP-Verbindung"
            self.l.unbind_s()
            self.ldapConnection = False
        else:
            Debug("Es gibt noch keine LDAP Verbidung, die zu schließen wäre")        
        
if __name__=='__main__':
    instance = SLUM()